package com.yunxin.admin.system.application.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunxin.admin.system.application.UserApplicationService;
import com.yunxin.admin.system.application.assembler.UserAssembler;
import com.yunxin.admin.system.application.command.*;
import com.yunxin.admin.system.application.dto.UpdateUserDetailDTO;
import com.yunxin.admin.system.domain.model.user.User;
import com.yunxin.admin.system.domain.model.user.UserMerchantRepository;
import com.yunxin.admin.system.domain.model.user.UserRepository;
import com.yunxin.admin.system.domain.model.user.UserRoleRepository;
import com.yunxin.admin.system.infra.common.constant.SystemConstants;
import com.yunxin.admin.system.infra.common.enums.AdminResultCode;
import com.yunxin.admin.system.infra.common.enums.UserTypeEnum;
import com.yunxin.admin.system.infra.framework.security.userdetails.SysUserDetails;
import com.yunxin.admin.system.infra.framework.security.util.SecurityUtils;
import com.yunxin.common.exception.BusinessException;
import com.yunxin.core.admin.domain.AdminTeam;
import com.yunxin.core.admin.domain.AdminUser;
import com.yunxin.core.admin.domain.AdminUserRole;
import com.yunxin.core.admin.domain.AdminUserTeamShip;
import com.yunxin.core.admin.service.AdminTeamService;
import com.yunxin.core.admin.service.AdminUserRoleService;
import com.yunxin.core.admin.service.AdminUserService;
import com.yunxin.core.admin.service.AdminUserTeamShipService;
import com.yunxin.core.application.command.merchant.OperateMerchantAccountReceivingStatusCommand;
import com.yunxin.core.application.command.merchant.OperateMerchantAccountStatusCommand;
import com.yunxin.core.application.command.merchant.OperateMerchantUserAccountCommand;
import com.yunxin.core.application.command.user.AddUserTeamCommand;
import com.yunxin.core.application.service.MerchantApplicationAdminService;
import com.yunxin.core.application.service.TeamService;
import com.yunxin.core.application.service.UserTeamShipService;
import com.yunxin.core.domain.external.merchant.MerchantExternalService;
import com.yunxin.core.domain.model.merchant.MerchantInfo;
import com.yunxin.core.domain.model.merchant.MerchantInfoRepository;
import com.yunxin.core.domain.model.team.Team;
import com.yunxin.core.domain.model.team.UserTeamShip;
import com.yunxin.core.domain.service.warn.IWarnHistoryService;
import com.yunxin.core.wechat.domain.WechatInfo;
import com.yunxin.core.wechat.domain.WechatInfoTeam;
import com.yunxin.core.wechat.domain.WechatUserSys;
import com.yunxin.core.wechat.service.QyWxService;
import com.yunxin.core.wechat.service.WechatInfoService;
import com.yunxin.core.wechat.service.WechatInfoTeamService;
import com.yunxin.core.wechat.service.WechatUserSysService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户业务实现类
 *
 * @author zero°
 * @since 2023-05-22
 */
@Service
@RequiredArgsConstructor
public class UserApplicationServiceImpl implements UserApplicationService {

    private final PasswordEncoder passwordEncoder;

    private final UserRepository userRepository;

    private final UserMerchantRepository userMerchantRepository;

    private final MerchantInfoRepository merchantInfoRepository;

    private final MerchantApplicationAdminService merchantApplicationAdminService;

    private final UserAssembler userAssembler;

    private final MerchantExternalService merchantExternalService;

    @Autowired
    private UserTeamShipService userTeamShipService;

    @Autowired
    private AdminUserTeamShipService adminUserTeamShipService;
    @Autowired
    @Lazy
    private QyWxService qyWxService;
    @Autowired
    private WechatInfoTeamService wechatInfoTeamService;
    @Autowired
    private WechatInfoService wechatInfoService;
    @Autowired
    private WechatUserSysService wechatUserSysService;
    @Autowired
    private AdminTeamService adminTeamService;
    @Autowired
    private AdminUserService adminUserService;


    @Override
    @DSTransactional
    public boolean saveUser(AddUserCommand command) {
        User user = userAssembler.formAddUserCommand(command);
//        user.setExport(command.getExport());
//        user.setIsView(command.getIsView());
        user.setUserType(UserTypeEnum.MERCHANT.getValue());
        // 设置默认加密密码
        String defaultEncryptPwd = passwordEncoder.encode(command.getPassword());

        // 处理AddUserTeamCommand
        //List<Long> teamIds = command.getUserTeamCommands().stream().map(AddUserTeamCommand::getTeamId).collect(Collectors.toList());

        // 新增用户
        boolean result = userRepository.saveUser(user, defaultEncryptPwd);
        if (result) {
            List<AddUserTeamCommand> userTeamCommands = command.getUserTeamCommands();
            // 保存用户团队
            userTeamShipService.saveUserTeamShip(user.getId(), userTeamCommands);
            //判断该用户是否是话务员
            if (SecurityUtils.X.isTelephonerUser()){
                for (AddUserTeamCommand userTeamCommand : userTeamCommands) {
                    if(userTeamCommand.getReceivingStatus()==1){
                        //判断该团队状态是否关闭 若关闭就开启
                        AdminTeam adminTeam = adminTeamService.getByTeamId(userTeamCommand.getTeamId().intValue());
                        if (ObjectUtils.isEmpty(adminTeam)){
                            throw new BusinessException("未找到资源");
                        }
                        MerchantInfo agoMerchantInfo = merchantInfoRepository.getMerchantInfoById(adminTeam.getMerchantId().longValue());
                        if (ObjectUtils.isEmpty(agoMerchantInfo)){
                            throw new BusinessException("未找到资源");
                        }
                        //判断团队状态是否关闭 是 开启
                        if (adminTeam.getStatus()==2){
                            adminTeamService.updateStatus(adminTeam.getId(),1);
                        }
                        //如果是开启接单状态 判断该商户下的接单状态是否关闭 是 开启商户接单状态
                        if (agoMerchantInfo.getType()==0){
                            MerchantInfo merchantInfo = new MerchantInfo();
                            merchantInfo.setId(agoMerchantInfo.getId());
                            merchantInfo.setType(1);
                            merchantInfo.setVersion(agoMerchantInfo.getVersion() + 1);
                            merchantInfoRepository.updateMerchantInfo(merchantInfo);
                        }
                    }
                }
            }
        }
        return result;
    }

    @Override
    @DSTransactional
    public boolean updateUser(UpdateUserCommand command) {
        AdminUser adminUser = adminUserService.getAdminById(command.getId());
        if (ObjectUtils.isEmpty(adminUser)){
            throw new BusinessException("未找到资源");
        }
        if (adminUser.getStatus()==0){
            throw new BusinessException("用户已禁用 请先解除禁用");
        }
        User user = userAssembler.formUpdateUserCommand(command);
        // 修改用户
//        user.setIsView(command.getIsView());
//        user.setExport(command.getExport());
        boolean result = userRepository.updateUser(user);
        if (result ) {
            // 先删除
            userTeamShipService.deleteUserTeamByUserId(command.getId());
            // 再保存用户团队
            userTeamShipService.saveUserTeamShip(command.getId(), command.getUserTeamCommands());

            List<AddUserTeamCommand> userTeamCommands = command.getUserTeamCommands();
            for (AddUserTeamCommand userTeamCommand : userTeamCommands) {
                AdminTeam adminTeam = adminTeamService.getByTeamId(userTeamCommand.getTeamId().intValue());
                if (ObjectUtils.isEmpty(adminTeam)){
                    throw new BusinessException("未找到资源");
                }
                MerchantInfo agoMerchantInfo = merchantInfoRepository.getMerchantInfoById(adminTeam.getMerchantId().longValue());
                if (ObjectUtils.isEmpty(agoMerchantInfo)){
                    throw new BusinessException("未找到资源");
                }
                //判断该用户是否是话务员
                if (SecurityUtils.X.isTelephonerUser()){
                    if (userTeamCommand.getReceivingStatus()==0){
                        //判断团队下的话务员是否全部关闭
                        List<UserTeamShip> userTeamShipsTeam = merchantExternalService.listReceivingStatusByTeamId(adminTeam.getId().longValue(), SystemConstants.BOKE_TELEPHONE);
                        if (userTeamShipsTeam.isEmpty() || (userTeamShipsTeam.size()==1 && userTeamShipsTeam.get(0).getUserMerchantId().intValue() != adminUser.getId())){
                            adminTeamService.updateStatus(adminTeam.getId(),2);
                        }
                        //如果是关闭接单状态 判断该商户下的话务员是否全部关闭  是的话关闭要的商户接单状态
                        List<UserTeamShip> userTeamShips = merchantExternalService.listReceivingStatusByMerchantId(agoMerchantInfo.getId(), SystemConstants.BOKE_TELEPHONE);
                        if (userTeamShips.isEmpty() || (userTeamShips.size()==1 && userTeamShips.get(0).getUserMerchantId().intValue() != adminUser.getId())){
                            MerchantInfo merchantInfo = new MerchantInfo();
                            merchantInfo.setId(agoMerchantInfo.getId());
                            merchantInfo.setType(0);
                            merchantInfo.setVersion(agoMerchantInfo.getVersion() + 1);
                            merchantInfoRepository.updateMerchantInfo(merchantInfo);
                        }
                    }else if (userTeamCommand.getReceivingStatus()==1){
                        //判断团队状态是否关闭 是 开启
                        if (adminTeam.getStatus()==2){
                            adminTeamService.updateStatus(adminTeam.getId(),1);
                        }
                        //如果是开启接单状态 判断该商户下的接单状态是否关闭 是 开启商户接单状态
                        if (agoMerchantInfo.getType()==0){
                            MerchantInfo merchantInfo = new MerchantInfo();
                            merchantInfo.setId(agoMerchantInfo.getId());
                            merchantInfo.setType(1);
                            merchantInfo.setVersion(agoMerchantInfo.getVersion() + 1);
                            merchantInfoRepository.updateMerchantInfo(merchantInfo);
                        }
                    }
                }
            }
        }
        return result;
    }

    @Override
    @DSTransactional
    public boolean saveMerchantUser(AddUserMerchantCommand command) {
        List<Long> merchantIds = userMerchantRepository.getUserMerchantIds(SecurityUtils.X.getUser().getUserId());
        if (merchantIds.isEmpty()) {
            throw new BusinessException(AdminResultCode.USER_MERCHANT_ERROR);
        }
        User user = userAssembler.formAddUserMerchantCommand(command);
        user.setUsername(command.getMobile());
        user.setUserType(UserTypeEnum.MERCHANT.getValue());
        user.setDeptId(SecurityUtils.X.getUser().getDeptId());
        //配置子账户角色
//        if (Objects.nonNull(command.getRole()) && command.getRole() == SystemConstants.DEFAULT_TEAM_ADMIN_ROLE_ID) {
//            user.setRoleIds(List.of(SystemConstants.DEFAULT_TEAM_ADMIN_ROLE_ID));
//        } else if (command.getRole() == SystemConstants.DEFAULT_TEAM_ADMIN_ROLE_ID){
//            user.setRoleIds(List.of(SystemConstants.DEFAULT_MERCHANT_USER_ROLE_ID));
//        }
        user.setRoleIds(command.getRoles());
        user.setMerchantId(merchantIds.get(0));
        user.setWeightId(command.getWeightId());
        // 设置默认加密密码
        String defaultEncryptPwd = passwordEncoder.encode(command.getPassword());
        // 新增用户
        boolean result = userRepository.saveUser(user, defaultEncryptPwd);
        if (result) {
            // 保存用户团队
            userTeamShipService.saveUserTeamShip(user.getId(), command.getUserTeamCommands());
            dealMerchantAccount(user, user.getMerchantId(), command.getRemark());
        }
        return result;
    }

    @Override
    @DSTransactional
    public boolean updateMerchantUser(UpdateUserMerchantCommand command) {
        List<Long> merchantIds = userMerchantRepository.getUserMerchantIds(SecurityUtils.X.getUser().getUserId());
        if (merchantIds.isEmpty()) {
            throw new BusinessException(AdminResultCode.USER_MERCHANT_ERROR);
        }
        User user = userAssembler.formUpdateUserMerchantCommand(command);
        user.setRoleIds(command.getRoles());
        user.setUsername(command.getMobile());
        // 修改用户
        boolean result = userRepository.updateUser(user);
        if (result) {
            // 先删除
            userTeamShipService.deleteUserTeamByUserId(command.getId());
            // 再保存用户团队
            userTeamShipService.saveUserTeamShip(command.getId(), command.getUserTeamCommands());
            dealMerchantAccount(user, merchantIds.get(0), command.getRemark());
        }
        return result;
    }

    @Override
    public Map<String,String> updateOwnUser(UpdateUserDetailDTO command) {
        User user = new User();
        BeanUtils.copyProperties(command,user);
        boolean result = userRepository.updateUser(user);
        Map<String,String> map =new HashMap<>();
        if (result){
            //修改团队企微手机号
            List<UpdateUserDetailDTO.teamShipDto> teamShipDtos = command.getTeamShipDtos();
            LambdaUpdateWrapper<AdminUserTeamShip> teamShipLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
            for (UpdateUserDetailDTO.teamShipDto teamShipDto : teamShipDtos) {
                AdminTeam team = adminTeamService.getById(teamShipDto.getTeamId());
                String whechatAccount = teamShipDto.getWhechatAccount();
                teamShipDto.setWhechatAccount(null);
                if (!ObjectUtils.isEmpty(whechatAccount)){
                    LambdaQueryWrapper<WechatInfoTeam> wechatInfoTeamLambdaQueryWrapper=new LambdaQueryWrapper<>();
                    wechatInfoTeamLambdaQueryWrapper.eq(WechatInfoTeam::getTeamId,teamShipDto.getTeamId());
                    WechatInfoTeam wechatInfoTeam = wechatInfoTeamService.getOne(wechatInfoTeamLambdaQueryWrapper);
                    if (!ObjectUtils.isEmpty(wechatInfoTeam)){
                        LambdaQueryWrapper<AdminUserTeamShip> queryWrapper=new LambdaQueryWrapper<>();
                        queryWrapper.eq(AdminUserTeamShip::getTeamId,teamShipDto.getTeamId())
                                .eq(AdminUserTeamShip::getUserMerchantId,command.getId())
                                .eq(AdminUserTeamShip::getWhechatAccount,whechatAccount);
                        long count = adminUserTeamShipService.count(queryWrapper);

                        if (count<1){
                            WechatInfo wechatInfo = wechatInfoService.getById(wechatInfoTeam.getWechatInfoId());
                            boolean flag = qyWxService.getUserByUserId(wechatInfo.getCorpid(), whechatAccount);
                            if (flag) {
                                LambdaQueryWrapper<WechatUserSys> wechatUserSysLambdaQueryWrapper=new LambdaQueryWrapper<>();
                                wechatUserSysLambdaQueryWrapper.eq(WechatUserSys::getWechatInfoId,wechatInfo.getId())
                                        .eq(WechatUserSys::getTeamId,teamShipDto.getTeamId().intValue())
                                        .eq(WechatUserSys::getSysUserId,command.getId().intValue());
                                WechatUserSys wechatUserSys = wechatUserSysService.getOne(wechatUserSysLambdaQueryWrapper);
                                if (ObjectUtils.isEmpty(wechatUserSys)){
                                    wechatUserSys = new WechatUserSys();
                                }
                                wechatUserSys.setTeamId(teamShipDto.getTeamId().intValue());
                                wechatUserSys.setWechatUserId(whechatAccount);
                                wechatUserSys.setWechatInfoId(wechatInfo.getId());
                                wechatUserSys.setCorpid(wechatInfo.getCorpid());
                                wechatUserSys.setSysUserId(command.getId().intValue());
                                wechatUserSysService.saveOrUpdate(wechatUserSys);
                                teamShipDto.setWhechatAccount(whechatAccount);
                            }else {
                                map.put(team.getTeamName(),"企微账号错误,绑定企微账号失败");
                            }
                        }
                    }
                }else {
                    map.put(team.getTeamName(),"该团队未绑定企微,绑定企微账号失败");
                }
                if (!ObjectUtils.isEmpty(teamShipDto.getWhechatAccount())){
                    teamShipLambdaUpdateWrapper.clear();
                    teamShipLambdaUpdateWrapper.eq(AdminUserTeamShip::getTeamId,teamShipDto.getTeamId())
                            .eq(AdminUserTeamShip::getUserMerchantId,command.getId())
                            .set(AdminUserTeamShip::getWhechatAccount,teamShipDto.getWhechatAccount());
                    adminUserTeamShipService.update(teamShipLambdaUpdateWrapper);
                }
            }
        }
        return map;
    }


    @Override
    @DSTransactional
    public boolean updateUserMerchantStatus(Long sysUserId, Integer status) {
        boolean result = userRepository.updateStatus(sysUserId, status);
        if (result) {
            OperateMerchantAccountStatusCommand command = new OperateMerchantAccountStatusCommand();
            command.setSysUserId(sysUserId);
            command.setAccountStatus(status);
            command.setOperator(SecurityUtils.X.getUser().getUsername());
            Boolean coreResult = merchantApplicationAdminService.operateMerchantAccountStatus(command);
            if (!coreResult){
                throw new BusinessException(AdminResultCode.USER_MERCHANT_OPERATE_ERROR);
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean updateUserMerchantReceivingStatus(Long sysUserId, Integer receivingStatus,Long merchantId) {
        OperateMerchantAccountReceivingStatusCommand command = new OperateMerchantAccountReceivingStatusCommand();
        command.setSysUserId(sysUserId);
        command.setReceivingStatus(receivingStatus);
        command.setOperator(SecurityUtils.X.getUser().getUsername());

//        warnHistoryService.checkByMerchant(merchantId);

        boolean b = merchantApplicationAdminService.operateMerchantAccountReceivingStatus(command, merchantId);

        return  b;
    }


    @Override
    @DSTransactional
    public boolean deleteUsers(String idsStr) {
        Assert.isTrue(CharSequenceUtil.isNotBlank(idsStr), "删除的用户数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong).toList();
        boolean result = userRepository.removeByIdList(ids);
//        if (result) {
//            merchantApplicationAdminService.deleteMerchantAccount(ids);
//        }
        return result;
    }


    @Override
    public boolean updatePassword(Long userId, String password) {
        return userRepository.updatePassword(userId, passwordEncoder.encode(password));
    }

    @Override
    public boolean updateStatus(Long userId, Integer status) {
        if (!ObjectUtils.isEmpty(status)){
            userRepository.updateStatus(userId, status);
        }
        //获取该用户所在团队
        if (SecurityUtils.X.isTelephonerUser()){
            if (status==0){
                List<AdminUserTeamShip> userTeamShips = adminUserTeamShipService.getListByUserId(userId.intValue());
                for (AdminUserTeamShip userTeamShip : userTeamShips) {
                    updateUserReceivingStatus(userId,userTeamShip.getTeamId().longValue(),0);
                }
            }
        }
        return true;
    }

    @Override
    public boolean updateUserReceivingStatus(Long userId, Long temaId, Integer receivingStatus) {
        AdminUser adminUser = adminUserService.getAdminById(userId);
        if (ObjectUtils.isEmpty(adminUser)){
            throw new BusinessException("未找到资源");
        }
        if (adminUser.getStatus()==0){
            throw new BusinessException("用户已禁用 请先解除禁用");
        }

        AdminTeam adminTeam = adminTeamService.getByTeamId(temaId.intValue());
        if (ObjectUtils.isEmpty(adminTeam)){
            throw new BusinessException("未找到资源");
        }
        MerchantInfo agoMerchantInfo = merchantInfoRepository.getMerchantInfoById(adminTeam.getMerchantId().longValue());
        if (ObjectUtils.isEmpty(agoMerchantInfo)){
            throw new BusinessException("未找到资源");
        }
        LambdaUpdateWrapper<AdminUserTeamShip> teamShipLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        teamShipLambdaUpdateWrapper.eq(AdminUserTeamShip::getTeamId,temaId)
                .eq(AdminUserTeamShip::getUserMerchantId,userId)
                .set(AdminUserTeamShip::getReceivingStatus,receivingStatus)
                .set(AdminUserTeamShip::getUpdateTime,new Date());
        adminUserTeamShipService.update(teamShipLambdaUpdateWrapper);

        if (SecurityUtils.X.isTelephonerUser()){
            if (receivingStatus==0){
                //判断团队下的话务员是否全部关闭
                List<UserTeamShip> userTeamShipsTeam = merchantExternalService.listReceivingStatusByTeamId(temaId, SystemConstants.BOKE_TELEPHONE);
                if (userTeamShipsTeam.isEmpty()){
                    adminTeamService.updateStatus(adminTeam.getId(),2);
                }
                //如果是关闭接单状态 判断该商户下的话务员是否全部关闭  是的话关闭要的商户接单状态
                List<UserTeamShip> userTeamShips = merchantExternalService.listReceivingStatusByMerchantId(agoMerchantInfo.getId(), SystemConstants.BOKE_TELEPHONE);
                if (userTeamShips.isEmpty()){
                    MerchantInfo merchantInfo = new MerchantInfo();
                    merchantInfo.setId(agoMerchantInfo.getId());
                    merchantInfo.setType(0);
                    merchantInfo.setVersion(agoMerchantInfo.getVersion() + 1);
                    boolean result = merchantInfoRepository.updateMerchantInfo(merchantInfo);
                }
            }else if (receivingStatus==1){
                //判断团队状态是否关闭 是 开启
                if (adminTeam.getStatus()==2){
                    adminTeamService.updateStatus(adminTeam.getId(),1);
                }
                //如果是开启接单状态 判断该商户下的接单状态是否关闭 是 开启商户接单状态
                if (agoMerchantInfo.getType()==0){
                    MerchantInfo merchantInfo = new MerchantInfo();
                    merchantInfo.setId(agoMerchantInfo.getId());
                    merchantInfo.setType(1);
                    merchantInfo.setVersion(agoMerchantInfo.getVersion() + 1);
                    boolean result = merchantInfoRepository.updateMerchantInfo(merchantInfo);
                }
            }
        }
        return true;
    }

    @Override
    public boolean updateReceivingStatus(UpdateReceivingStatusCommand command) {
        LambdaUpdateWrapper<AdminUserTeamShip> teamShipLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        teamShipLambdaUpdateWrapper.in(AdminUserTeamShip::getTeamId,command.getTeamIdList())
                .set(AdminUserTeamShip::getReceivingStatus,command.getReceivingStatus());
        return adminUserTeamShipService.update(teamShipLambdaUpdateWrapper);
    }


    /**
     * 处理用户角色和商户
     *
     * @param user       user
     * @param merchantId merchantId
     * @param remark     remark
     */
    private void dealMerchantAccount(User user, Long merchantId, String remark) {
        OperateMerchantUserAccountCommand command = getOperateMerchantUserAccountCommand(user, merchantId, remark);
        Boolean result = merchantApplicationAdminService.operateMerchantUserAccount(command);
        if (!result) {
            throw new BusinessException(AdminResultCode.USER_MERCHANT_OPERATE_ERROR);
        }

    }

   /**
    * 获取操作商户用户command
    * @param user user
    * @param merchantId sysUser
    * @param remark remark
    * @return OperateMerchantUserAccountCommand
    */
   @NotNull
    private OperateMerchantUserAccountCommand getOperateMerchantUserAccountCommand(User user, Long merchantId, String remark) {
        OperateMerchantUserAccountCommand request = new OperateMerchantUserAccountCommand();
        request.setSysUserId(user.getId());
        request.setAccountName(user.getNickname());
        request.setAccountPhone(user.getMobile());
        request.setMerchantId(merchantId);
        SysUserDetails userDetails = SecurityUtils.X.getUser();
        if (SecurityUtils.X.isBokeMerchantUser()) {
            request.setOperateSysUserId(userDetails.getUserId());
        }
        request.setOperator(userDetails.getUsername());
        request.setRemark(remark);
        request.setAccountStatus(user.getStatus());
        return request;
    }

}
